#include "StdAfx.h"
#include "MVanillaOptionPricer.h"
#include <exception>

namespace TreasAlgo
{

MVanillaOptionPricer::MVanillaOptionPricer()
	: m_optPtr(new TsVanillaOptionPricer())
{
}

MVanillaOptionPricer::~MVanillaOptionPricer()
{	
}

void MVanillaOptionPricer::priceOptionEuro(
						DateTime	valueDate,
						DateTime	maturity,
						double		underlying,
						double		strike,
						double		riskFreeRate,
						double		dividendYield,
						double		volatility,
						double%		NPV,
						double%		delta,
						double%		gamma,
						double%		theta)
{

	try
	{
		Date valDate((int)valueDate.ToOADate());
		Date matDate((int)maturity.ToOADate());

		double nVal = NPV;
		double dVal = delta;
		double gVal = gamma;
		double tVal = theta;

		m_optPtr->priceOptionEuro(valDate,
								  matDate,
								  underlying,
								  strike,
								  riskFreeRate,
								  dividendYield,
								  volatility,
								  nVal,
								  dVal,
								  gVal,
								  tVal);
		NPV = nVal;
		delta = dVal;
		gamma = gVal;
		theta = tVal;
	}
	catch(const std::exception & ex)
	{
		String ^errorMessage = String::Format(L"Exception thrown while pricing European option: {0}", gcnew String(ex.what()));
		throw gcnew Exception(errorMessage);
	}
	catch(Exception ^e)
	{
		throw e;
	}
	catch(...)
	{
		throw gcnew Exception("Unexpected failure while calculating European option price.");
	}
}

void MVanillaOptionPricer::priceOptionAmer(
						DateTime	valueDate,
						DateTime	maturity,
						double		underlying,
						double		strike,
						double		riskFreeRate,
						double		dividendYield,
						double		volatility,
						double%		NPV,
						double%		delta,
						double%		gamma)
{
	try
	{
		Date valDate((int)valueDate.ToOADate());
		Date matDate((int)maturity.ToOADate());

		double nVal = NPV;
		double dVal = delta;
		double gVal = gamma;
		
		m_optPtr->priceOptionAmer(valDate,
								  matDate,
								  underlying,
								  strike,
								  riskFreeRate,
								  dividendYield,
								  volatility,
								  nVal,
								  dVal,
								  gVal);

		NPV = nVal;
		delta = dVal;
		gamma = gVal;
	}
	catch(const std::exception & ex)
	{
		String ^errorMessage = String::Format(L"Exception thrown while pricing American option: {0}", gcnew String(ex.what()));
		throw gcnew Exception(errorMessage);
	}
	catch(Exception ^e)
	{
		throw e;
	}
	catch(...)
	{
		throw gcnew Exception("Unexpected failure while calculating American option price.");
	}
}

}
